//
//  CheckPermissionsTool.m
//  JS_LDZX
//
//  Created by 赵博 on 2019/3/27.
//  Copyright © 2019 VJSPForJS_LDZX. All rights reserved.
//

#import "CheckPermissionsTool.h"
#import <AVFoundation/AVFoundation.h>
#import <UIKit/UIKit.h>
#import <CoreLocation/CLLocationManager.h>
#import <Photos/Photos.h>
@implementation CheckPermissionsTool


#pragma mark - 麦克风
- (BOOL)checkMicrophonePermission{
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
    switch (authStatus) {
        case AVAuthorizationStatusNotDetermined:
            //没有询问是否开启麦克风
            return NO;
            break;
        case AVAuthorizationStatusRestricted:
            //未授权，家长限制
            break;
        case AVAuthorizationStatusDenied:
            //玩家未授权
            break;
        case AVAuthorizationStatusAuthorized:
            //玩家授权
            return NO;
            break;
        default:
            break;
    }
    return YES;
}

#pragma mark - 摄像头
- (BOOL)checkCameraPermission{
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    switch (authStatus) {
        case AVAuthorizationStatusNotDetermined:
            //没有询问是否开启摄像头
            return NO;
            break;
        case AVAuthorizationStatusRestricted:
            //未授权，家长限制
            break;
        case AVAuthorizationStatusDenied:
            //玩家未授权
            break;
        case AVAuthorizationStatusAuthorized:
            //玩家授权
            return NO;
            break;
        default:
            break;
    }
    return YES;
}
- (void)showAlertWithTitle:(NSString *)title type:(NSInteger)type vc:(UIViewController *)vc{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:[self msgCopy:type] preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        }];
        
        [alertVC addAction:okAction];
        
        [vc presentViewController:alertVC animated:NO completion:nil];
    });

}

#pragma mark - 相册权限
- (BOOL)checkAlbumPermission{
    PHAuthorizationStatus authStatus = [PHPhotoLibrary authorizationStatus];

    switch (authStatus) {
        case PHAuthorizationStatusNotDetermined:
            return NO;
            break;
        case PHAuthorizationStatusRestricted:
            
            break;
        case PHAuthorizationStatusDenied:
            
            break;
        case PHAuthorizationStatusAuthorized:
            //玩家授权
            return NO;
            break;
        default:
            break;
    }
     return YES;
}

#pragma mark - 定位权限
- (BOOL)checkLoactionPermission{
    CLAuthorizationStatus authStatus = [CLLocationManager authorizationStatus];
    switch (authStatus) {
        case kCLAuthorizationStatusNotDetermined:
            return NO;
            break;
        case kCLAuthorizationStatusRestricted:
            //未授权，家长限制
            break;
        case kCLAuthorizationStatusAuthorizedAlways:
            //一直允许获取定位
            return NO;
            break;
        case kCLAuthorizationStatusAuthorizedWhenInUse:
            //在使用时允许获取定位
            return NO;
            break;
        case kCLAuthorizationStatusDenied:
            //未授权，家长限制
            break;
        default:
            break;
    }
    return YES;
}
- (BOOL)checkReadPhotoAlbumPermission{
    return YES;
}
- (BOOL)checkWirtePhotoAlbumPermission{
    return YES;
}


- (NSString *)msgCopy:(NSInteger) type{
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *app_Name = [infoDictionary objectForKey:@"CFBundleDisplayName"];
    if(type == 0){
        return [NSString stringWithFormat:@"请在iPhone的”设置-隐私-麦克风“选项中，允许%@访问你的手机麦克风",app_Name];
    }else if (type == 1){
         return [NSString stringWithFormat:@"请在iPhone的”设置-隐私-摄像头“选项中，允许%@访问你的手机摄像头",app_Name];
    }else if (type == 2){
        return [NSString stringWithFormat:@"请在iPhone的”设置-隐私-位置“选项中，允许%@访问你的手机位置",app_Name];
    }else if (type == 3){
         return [NSString stringWithFormat:@"请在iPhone的”设置-隐私-相册“选项中，允许%@访问你的手机相册",app_Name];
    }
    return @"";
}

+ (CheckPermissionsTool *)sharedView {
    static dispatch_once_t once;
    static CheckPermissionsTool *sharedView;
    dispatch_once(&once, ^ {
        sharedView = [[CheckPermissionsTool alloc] init];
    });
    return sharedView;
}

@end
